package com.score.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import com.score.mapper.IStudentMapper;
import com.score.mapper.IUserMapper;
import com.score.pojo.ReturnEntry;
import com.score.pojo.TStudent;
import com.score.pojo.TUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StucadresServiceImpl {

    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private IStudentMapper studentMapper;


    public ReturnEntry getAllStucadres(String college, Integer currentPage, Integer pageSize) {
        QueryWrapper<TUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("role_id", 3);
        Page<TUser> tUserPage = userMapper.selectPage(new Page<>(currentPage, pageSize), userQueryWrapper);
        List<String> usernameList = new ArrayList<>();
        QueryWrapper<TStudent> studentQueryWrapper = new QueryWrapper<>();
        List<TStudent> stuList = new ArrayList<>();
        for (TUser user : tUserPage.getRecords()) {
            usernameList.add(user.getUsername());
        }
        for (String userNum : usernameList) {
            studentQueryWrapper.clear();
            studentQueryWrapper.eq(!StringUtils.isNullOrEmpty(college), "college", college)
                    .eq(!StringUtils.isNullOrEmpty(userNum), "stu_num", userNum);
            TStudent student = studentMapper.selectOne(studentQueryWrapper);
            if (student != null)
                stuList.add(student);
        }
        return new ReturnEntry().ok(stuList, college + "的查寝干部");
    }

    public ReturnEntry addStucadres(String username, String name, String college, String password, Integer roleId) {
        TUser user = new TUser(username, password, roleId);
        ReturnEntry returnEntry = addOne(user);
        if (returnEntry.getCode() == 200) {
            TStudent student = new TStudent();
            student.setStuNum(username);
            student.setName(name);
            student.setCollege(college);
            int insert = studentMapper.insert(student);
            if (insert != 1)
                throw new RuntimeException("添加失败");
            return new ReturnEntry().ok("添加成功", student);
        }
        return new ReturnEntry().fail("添加失败", null);
    }

    private BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

    private ReturnEntry addOne(TUser user) {
        QueryWrapper<TUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername());
        TUser tUser = userMapper.selectOne(wrapper);
        if (tUser != null)
            return new ReturnEntry().fail("创建失败", "用户名已存在");
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        int insert = userMapper.insert(user);
        if (insert == 1)
            return new ReturnEntry().ok("添加成功", user.getUsername(), user.getRoleId());
        return new ReturnEntry().fail("添加失败", null);
    }

    public ReturnEntry deleteStucadres(List<Integer> ids) {
        int temp = 0;
        QueryWrapper<TUser> wrapper = new QueryWrapper<>();
        List<TStudent> students = studentMapper.selectBatchIds(ids);
        for (TStudent student : students) {
            wrapper.clear();
            wrapper.eq("username", student.getStuNum());
            userMapper.delete(wrapper);
            studentMapper.deleteById(student.getId());
            temp++;
        }
        if (temp != ids.size())
            throw new RuntimeException("删除失败");
        return new ReturnEntry().ok(ids, "删除成功，删除了：" + temp + "条数据");
    }
}
